package com.java.al.swing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class FileChooserText extends JFrame {
	private JTextField
		fileName = new JTextField(),
		dir = new JTextField();
	private JButton
		open = new JButton("open"),
		save = new JButton("save");
	private JFileChooser fileChooser = new JFileChooser();
	private java.util.List strList = new ArrayList();
	public FileChooserText(){

	    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
	    fileChooser.addChoosableFileFilter(new MyFilter());
		
		JPanel p = new JPanel();
		open.addActionListener( new OpenL());
		p.add(open);
		save.addActionListener( new SaveL());
		p.add(save);
		add(p, BorderLayout.SOUTH);
		dir.setEditable(false);
		fileName.setEditable(false);
		p = new JPanel();
		p.setLayout( new GridLayout(2, 1));
		p.add(fileName);
		p.add(dir);
		add(p, BorderLayout.NORTH);
	}
	class OpenL implements ActionListener{
		public void actionPerformed(ActionEvent e){
			int rVal = fileChooser.showOpenDialog(FileChooserText.this);
			if(rVal == JFileChooser.APPROVE_OPTION){
				fileName.setText(fileChooser.getSelectedFile().getName());
				dir.setText(fileChooser.getSelectedFile().getAbsolutePath());
				try {
					File file = fileChooser.getSelectedFile();
					InputStream in = new FileInputStream(file);
					BufferedReader reader =new BufferedReader(new InputStreamReader(in));
					String str=reader.readLine();
					while(null!=str ){
						System.err.println(str);
						str=reader.readLine();
						strList.add(str);
					}
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(rVal == JFileChooser.CANCEL_OPTION){
				fileChooser.setSelectedFile(null);
				fileName.setText("You pressed cancel!");
				dir.setText("");
			}
		}
	}
	class SaveL implements ActionListener{
		public void actionPerformed(ActionEvent e){

			int rVal = fileChooser.showSaveDialog(FileChooserText.this);
			if(rVal == JFileChooser.APPROVE_OPTION){
				fileName.setText(fileChooser.getSelectedFile().getName());
				dir.setText(fileChooser.getCurrentDirectory().toString());
				System.out.println("dir = " + fileChooser.getCurrentDirectory().toString()+File.separator+fileChooser.getSelectedFile().getName());
				try {
					File file = fileChooser.getSelectedFile();
					PrintWriter out = new PrintWriter(new FileOutputStream(file.getAbsoluteFile()));
					for (int i=0; i<strList.size(); i++) {
						String str = (String) strList.get(i) + "\n";
			        	out.write((str == null ? "" : str));
			        }
					out.flush();
			        out.close();
			    } catch (IOException ie) {
			    	
			    }
			}
			if(rVal == JFileChooser.CANCEL_OPTION){
				fileChooser.setSelectedFile(null);
				fileName.setText("You pressed cancel!");
				dir.setText("");
			}
		}
	}
	private class MyFilter extends javax.swing.filechooser.FileFilter {
		  public boolean accept(File file) {
		    String filename = file.getName();
		    return filename.toLowerCase().endsWith(".txt");
		  }

		  public String getDescription() {
		    return "文本文档(*.txt)";
		  }
		}
	public static void main(String[] args) {
		SwingConsole.run( new FileChooserText(), 250, 150);
	}
}
